package org.wzl.service.controller;

import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotBlank;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.wzl.service.dto.UserLoginDTO;
import org.wzl.service.dto.UserRegisterDTO;
import org.wzl.service.exception.BusinessException;
import org.wzl.service.service.UserLoginService;
import org.wzl.service.utils.Resp;

import java.io.IOException;

/**
 * @author: 卫志龙
 * @date: 2024年06月26日 15:06
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/user_login")
public class UserLoginController {

    @Resource
    private UserLoginService userLoginService;

    @GetMapping("/getVerifyImg")
    public Resp<String> getVerifyImg(@NotBlank(message = "请输入code") @RequestParam String code) {
        String base64;
        try {
            base64 = userLoginService.getVerifyImg(code);
        } catch (Exception e) {
            log.error("获取验证码图片失败", e);
            return Resp.fail("获取验证码失败");
        }
        return Resp.ok(base64);
    }

    @PostMapping("/login")
    public Resp<String> login(@RequestBody @Validated UserLoginDTO userLoginDTO) {
        return Resp.ok(userLoginService.login(userLoginDTO));
    }

    @PostMapping("/register")
    public Resp<?> register(@RequestBody @Validated UserRegisterDTO userRegisterDTO) {
        userLoginService.register(userRegisterDTO);
        return Resp.ok();
    }

    @GetMapping("/logout")
    public Resp<?> logout() {
        userLoginService.logout();
        return Resp.ok();
    }

}
